package cn.lmxdev.controller;

import cn.lmxdev.model.httpVO.FaceCodeVO;
import cn.lmxdev.service.IFaceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.HashMap;

@Controller
public class TestController {
    @Autowired
    private IFaceService faceService = null;

    @PostMapping(value = "/face_data_get")
    @ResponseBody
    public FaceCodeVO faceDataGet(@RequestParam(value = "faceBase64") String faceBase64) {
        return faceService.getFaceCode(faceBase64, "hog");
    }

    @PostMapping(value = "/face_data_contrast")
    @ResponseBody
    public HashMap<String, Object> faceDataContrast(
            @RequestParam(value = "face_data_a") ArrayList<Double> faceDataA,
            @RequestParam(value = "face_data_b") ArrayList<Double> faceDataB,
            @RequestParam(value = "max_error") Double maxError) {
        HashMap<String, Object> hashMap = new HashMap<>();
        if (faceDataA.size() == 128 && faceDataB.size() == 128) {
            Double faceValue = 0.0;
            for (int i = 0; i < 128; i++)
                faceValue += Math.pow(faceDataA.get(i) - faceDataB.get(i), 2);
            hashMap.put("code", 0);
            if (Math.sqrt(faceValue) <= maxError)
                hashMap.put("msg", true);
            else
                hashMap.put("msg", false);
        } else {
            hashMap.put("code", -1);
            hashMap.put("msg", "人脸数据不完整");
        }
        return hashMap;
    }

    @GetMapping(value = "/index")
    public String index() {
        return "comparison";
    }
}
